package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.dto.AdminShareProfitCollectDetailsDTO;
import com.sc.nft.dto.AdmnTypeSummaryDTO;
import com.sc.nft.dto.MarketValueManagementDaoDTO;
import com.sc.nft.dto.SimpleDigitalGetSeparateFundDTO;
import com.sc.nft.enums.EcologyHatchShareProfitStatus;
import com.sc.nft.enums.EcologyHatchShareProfitTypeEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.EcologyHatchShareProfit;
import com.sc.nft.mapper.EcologyHatchShareProfitMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 生态-DAO分成记录表(sc_ecology_hatch_share_profit)数据DAO
 *
 * @author Sun
 * @since 2023-12-06 18:48:04
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class EcologyHatchShareProfitDao extends ServiceImpl<EcologyHatchShareProfitMapper, EcologyHatchShareProfit> {

    public AdminShareProfitCollectDetailsDTO getSumDistributionPointsGroupTypeByCollectionId(Long collectionId) {
        return baseMapper.getSumDistributionPointsGroupTypeByCollectionId(collectionId);
    }

    public AdminShareProfitCollectDetailsDTO shareProfitCollectDetailsByUserRecordId(Long id) {
        return baseMapper.shareProfitCollectDetailsByUserRecordId(id);
    }

    public Integer getCountByUserRecordId(Long userRecordId) {
        LambdaQueryWrapper<EcologyHatchShareProfit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchShareProfit::getUserRecordId, userRecordId);
        return count(wrapper);
    }

    /**
     * 获取上周分红池
     * @return
     */
    public BigDecimal getLastWeekBonusPool(Long daoId, EcologyHatchShareProfitTypeEnum type) {
        DateTime beginOfWeek = DateUtil.beginOfWeek(DateUtil.lastWeek());
        DateTime endOfWeek = DateUtil.endOfWeek(DateUtil.lastWeek());
        return baseMapper.getLastWeekBonusPool(daoId,type.getCode(),beginOfWeek,endOfWeek);
    }

    public List<EcologyHatchShareProfit> getListByType(EcologyHatchShareProfitTypeEnum type) {
        LambdaQueryWrapper<EcologyHatchShareProfit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchShareProfit::getType, type);
        wrapper.eq(EcologyHatchShareProfit::getSettleStatus, EcologyHatchShareProfitStatus.TO_BE_PROCESSED);
        wrapper.le(EcologyHatchShareProfit::getSettleTimeStamp, Long.parseLong(DateUtil.format(DateUtil.beginOfDay(DateTime.now()), "yyyyMMddHH")));
        return list(wrapper);
    }

    public boolean updateSettleStatusById(Long id, EcologyHatchShareProfitStatus settleStatus) {
        LambdaUpdateWrapper<EcologyHatchShareProfit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchShareProfit::getId,id);
        wrapper.set(EcologyHatchShareProfit::getSettleStatus,settleStatus);
        return update(new EcologyHatchShareProfit(),wrapper);
    }

    public boolean updateAllSettleStatus(EcologyHatchShareProfitStatus status) {
        LambdaUpdateWrapper<EcologyHatchShareProfit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EcologyHatchShareProfit::getSettleStatus,status);
        return update(new EcologyHatchShareProfit(),wrapper);
    }

    public List<MarketValueManagementDaoDTO> getSettleDaoListByTypeAndLastMonth(EcologyHatchShareProfitTypeEnum type) {
        DateTime beginMonth = DateUtil.beginOfMonth(DateTime.now());
        DateTime endnMonth = DateUtil.endOfMonth(DateTime.now());
//        DateTime beginMonth = DateUtil.beginOfMonth(DateUtil.lastMonth());
//        DateTime endnMonth = DateUtil.endOfMonth(DateUtil.lastMonth());

        return baseMapper.getSettleDaoListByTypeAndLastMonth(type.getCode(),beginMonth,endnMonth);
    }

    /**
     * 获取冻结生态分润记录
     * @return
     */
    public List<EcologyHatchShareProfit> getFrozenList(){
        return baseMapper.getFrozenList();
    }

    /**
     * 修改池Id
     * @param ids
     * @param poolId
     * @return
     */
    public boolean updatePoolIdById(List<Long> ids,Long poolId) {
        LambdaUpdateWrapper<EcologyHatchShareProfit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(EcologyHatchShareProfit::getId,ids);
        wrapper.set(EcologyHatchShareProfit::getMarketManagementDaoPoolId,poolId);
        return update(new EcologyHatchShareProfit(),wrapper);
    }

    /**
     * 获取给本dao分过账的数据
     * @param daoId
     * @return
     */
    public List<SimpleDigitalGetSeparateFundDTO> getDaoSeparateFund(Long daoId) {
        return baseMapper.getDaoSeparateFund(daoId);
    }
}